package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;

import java.util.List;

import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.DeleteVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowBusiAddVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowBusiUpdateVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowBusiRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowModelRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowModelQueryInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Controller - 业务
 *
 * @author lilh
 * @date 2019-01-22 17:17
 */
@RestController
@RequestMapping("/busi")
public class FlowBusiController {

    @Autowired
    private FlowBusiRepository flowBusiRepository;

    @Autowired
    private FlowModelRepository flowModelRepository;

    /**
     * 增加
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody @Validated(value = FlowBusiAddVo.ValidatedAdd.class) FlowBusiAddVo flowBusiAddVo) {
        Integer insert = flowBusiRepository.insert(flowBusiAddVo);
        if (insert > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.fail(ErrorCode.ALREADY_EXIST.errCode(), "应用不存在或业务名称重复");
    }

    /**
     * 更新
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Validated(value = FlowBusiAddVo.ValidatedUpdate.class) FlowBusiUpdateVo flowBusiUpdateVo,
                                 BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseResult.fail(ErrorCode.PARAMETER_ERROR.errCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return ResponseResult.success(flowBusiRepository.update(flowBusiUpdateVo));
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody DeleteVo deleteVo) {
        ModelQueryVo modelQueryVo = new ModelQueryVo();
        modelQueryVo.setAppId(deleteVo.getAppId());
        modelQueryVo.setBusiId(deleteVo.getBusiId());
        modelQueryVo.setTenantId(deleteVo.getTenantId());
        ResponseResult models = flowModelRepository.list(modelQueryVo);
        if (models.isSuccess()) {
            List<FlowModelQueryInfo> result = (List<FlowModelQueryInfo>) models.getDatas();
            if (CollectionUtils.isNotEmpty(result)) {
                return ResponseResult.fail(ErrorCode.BUSI_HAS_MODEL.errCode(), ErrorCode.BUSI_HAS_MODEL.errMessage());
            }
        }
        return ResponseResult.success(flowBusiRepository.delete(deleteVo.getBusiId()));
    }

}
